<?php
/**
 * Created by PhpStorm.
 * User: 勇林
 * Date: 2015/11/11
 * Time: 14:54
 */

namespace app\common\utils;


class HttpClient
{

    public static function get($url){

        //初始化
        $ch = curl_init();
        //设置选项，包括URL
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        $data = curl_exec($ch);
        curl_close($ch);
        return json_decode($data);


    }

    static public function post($url, $params = null){

        $ch = curl_init ();
        curl_setopt ( $ch, CURLOPT_URL, $url );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 10 );
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt ( $ch, CURLOPT_POST, 1 ); //启用POST提交
        if($params != null){
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        }
        $data = curl_exec ( $ch );
        curl_close ( $ch );

        return json_decode($data);

    }

}